import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'pages/jj_tabs_first_pages/jj_cart.dart';
import 'pages/jj_tabs_first_pages/jj_category.dart';
import 'pages/jj_tabs_first_pages/jj_home.dart';
import 'pages/jj_tabs_first_pages/jj_user.dart';
class Tabs extends StatefulWidget {
  const Tabs({Key? key}) : super(key: key);

  @override
  _TabsState createState() => _TabsState();
}

class _TabsState extends State<Tabs> {
  PageController _pageController=PageController();//=PageController()因为空安全加的

 void initState() { 
    super.initState();
    this._pageController=new PageController(initialPage:this._currentIndex );
  }


  var _pageList = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    User()
  ];
  int _currentIndex = 0;
  @override
  Widget build(BuildContext context) {
     ScreenUtil.init(
         BoxConstraints(
             maxWidth: MediaQuery.of(context).size.width,
             maxHeight: MediaQuery.of(context).size.height
         ),
         designSize: Size(360, 720),
         minTextAdapt: true,
         orientation: Orientation.portrait
     );
      return MediaQuery(
        data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
        child: Scaffold(

          body: PageView(
              controller: this._pageController,
              children: this._pageList,
               onPageChanged: (index) {
              setState(() {
                this._currentIndex = index;
              });
            },
            ),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: this._currentIndex,
           onTap: (index){
                  setState(() {
                     this._currentIndex=index;
                     this._pageController.jumpToPage(index);
                  });
              },
            type: BottomNavigationBarType.fixed,
            items: [
              BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("首页")),
              BottomNavigationBarItem(icon: Icon(Icons.cabin), title: Text("分类")),
              BottomNavigationBarItem(
                  icon: Icon(Icons.holiday_village), title: Text("购物车")),
              BottomNavigationBarItem(
                  icon: Icon(Icons.access_alarm), title: Text("个人中心")),
            ],
          ),
        ),
      );


  }
}
